$(document).ready(function() {
    dataSource = new kendo.data.DataSource({
        transport: {
            create: {
                url: appBosch.crud.create,
                dataType: "json"
            },
            read: {
                url: appBosch.crud.read,
                dataType: "json"
            },
            update: {
                url: appBosch.crud.update,
                dataType: "json"
            },
            destroy: {
                url: appBosch.crud.destroy,
                dataType: "json"
            },
            parameterMap: function(options, operation) {
                var params = {};
                switch (operation)
                {
                    case 'create':
                        params = {models: kendo.stringify(options.models)};
                    case 'read':
                        break;
                    case 'update':
                        params = {models: kendo.stringify(options.models)};
                        break;
                    case 'destroy':
                        params = {id: options.models[0].value};
                }
                return params;
            }
        },
        batch: true,
        schema: {
            model: {
                id: "value",
                fields: {
                    text: {
                        editable: true,
                        nullable: false,
                        validation: {
                            required: true,
                            min: 1
                        }
                    },
                    capacity: {
                        type: "number",
                        editable: true,
                        nullable: true
                    }
                }
            }
        }
    });

    $("#grid").kendoGrid({
        toolbar: [
            {
                name: "create",
                text: "Agregar sala"
            },
            {
                name: "excel",
                text: "Exportar a Excel"
            },
            {
                name: "pdf",
                text: "Exportar a PDF"
            }
        ],
        excel: {
            allPages: true,
            fileName: "Salas.xlsx",
            filterable: true
        },
        pdf: {
            allPages: true,
            fileName: "Salas.pdf"
        },
        scrollable: true,
        filterable: true,
        selectable: true,
        navigatable: true,
        resizable: true,
        columnMenu: true,
        dataSource: dataSource,
        columns: [
            {field: "text", title: "Nombre" },
            {field: "capacity", title: "Cupo" },
            {command: [
                {
                    name: "edit",
                    text: {
                        edit: "",
                        update: "",
                        cancel: ""
                    }
                },
                {
                    name: "destroy",
                    text: ""
                }], title: "&nbsp;"}],
        editable: {
            confirmation: "¿Estas seguro que deseas eliminar esta sala?",
            mode: "inline"
        }
    });
});

function preview(e) {
    $("#background").css("background-color", e.value);
}